package cn.kwq.project_manger.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.kwq.project_manger.common.R;
import cn.kwq.project_manger.common.RES;
import cn.kwq.project_manger.exception.UnLockWyuSystemException;
import cn.kwq.project_manger.pojo.User;
import cn.kwq.project_manger.service.UserService;
import cn.kwq.project_manger.utils.AesUtils;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "用户服务")
@RestController
@RequestMapping("/user/")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    AesUtils aes;


//    /**
//     * @param object
//     * @return
//     */
//    @ApiOperation("发送验证码邮箱接口")
//    @SaCheckLogin
//    @LimitRequest(msg = "一分钟内只能请求一次，请稍后再试")
//    @ApiResponses({
//            @ApiResponse(code=4100,message="超过请求次数相应的错误信息"),
//    })
//    @PostMapping("sendRandom")
//    public RES SendRandom(@ApiParam(name = "email", value = "用户邮箱，前端正则校验格式", required = true)
//                          @RequestBody JSONObject object ) {
//            String email = (String) object.get("email");
//            String random = emailUtils.sendRandom(email);
//            return R.okm("random",random);
//    }

//    /**
//     * @param object
//     * @return
//     */
//    @ApiOperation("绑定邮箱接口")
//    @SaCheckLogin
//    @PostMapping("bindEmail")
//    public RES BindEmail(@ApiParam(name = "email", value = "用户邮箱，前端正则校验格式", required = true)
//                          @RequestBody JSONObject object ) {
//            String email = (String) object.get("email");
//            User user = userService.bindEmail(email, StpUtil.getLoginIdAsLong());
//            return R.ok(user);
//
//    }

    /**
     * @param object
     * @return
     */
    @ApiOperation("更改用户昵称")
    @SaCheckLogin
    @PostMapping("updateNickName")
    public RES updateNickName(@ApiParam(name = "nickName", value = "用户昵称，前端限定长度", required = true)
                         @RequestBody JSONObject object ) {

        String nickName = (String) object.get("nickName");
        User user = userService.updateMyNickName(nickName, StpUtil.getLoginIdAsLong());
        log.info(user.getUserName()+" 更改昵称为："+nickName);
        return R.ok("更改成功",user);
    }



    /**
     * @return
     */
    @ApiOperation("获取当前用户信息（openid未解密）")
    @SaCheckLogin
    @GetMapping("getUser")
    public RES getUser() {
        User user = userService.getUserById(StpUtil.getLoginIdAsLong());
        return R.ok(user);
    }

    /**
     * @return
     */
    @ApiOperation("单独获取当前用户解密后openid")
    @SaCheckLogin
    @GetMapping("getUserOpenid")
    public RES getUseOpenid() {
        User user = userService.getUserById(StpUtil.getLoginIdAsLong());
        String openid = user.getOpenid();
        //解密openid
        return R.ok(aes.AES2S(openid));
    }

    @ApiOperation("解绑当前用户的教务系统信息")
    @SaCheckLogin
    @GetMapping ("unLockWyu")
    public RES unLockWyu() throws UnLockWyuSystemException {
        User user = userService.unLockWyu(StpUtil.getLoginIdAsLong());
        log.info("["+"用户: "+StpUtil.getLoginIdAsString()+"]"+" "+"解绑成功");
        return R.ok("解绑成功",user);
    }

    /**
     * @return
     */
    /*@ApiOperation("为简历获取WYU信息")
    @SaCheckLogin
    @GetMapping("getMsgForResume")
    public RES getMsgForResume() {
        return R.ok(userService.getMsgForResume(StpUtil.getLoginIdAsLong()));
    }*/




}
